package com.weishu.housing.sms;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.weishu.common.core.web.domain.AjaxResult;
import com.weishu.core.mapper.SysUserMapper;
import com.weishu.housing.utils.CodeUtils;
import com.weishu.housing.utils.SendCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Author wangyaohan
 * @Date 2021/10/20 7:58
 * @ClassName SmsServiceImpl
 */
@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public AjaxResult sendCode(String phone) throws ClientException {
        //未处理安全问题
        /*if (sysUserMapper.getByPhone(phone)!=null){
            return AjaxResult.error("该手机号已注册！");
        }*/
        try {
            //发送短信
            SendSmsResponse send = SendCodeUtils.sendCode(phone);
            //验证码存入redis 2分钟有效
            redisTemplate.opsForValue().setIfAbsent(phone,send.getCode(),2, TimeUnit.MINUTES);
            return AjaxResult.success("发送成功！2分钟有效！");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(500,"发送失败！");
        }
    }
}
